package com.maixun.lib_choosefile.adapter

import android.content.Context
import android.net.Uri
import android.text.format.DateFormat
import android.text.format.Formatter
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.maixun.lib_choosefile.entity.LocalFileIm
import com.maixun.lib_choosefile.LocalFileConst
import com.maixun.lib_choosefile.R
import java.io.File

/**
 * @author：Lmoumou
 * @date：2020/9/30
 */
class LocalFileAdapter(
    private val mContext: Context,
    private val type: Int,
    private val dataList: MutableList<LocalFileIm>,
    private val onItemClick: (item: LocalFileIm, selectView: ImageView, position: Int) -> Unit
) :
    RecyclerView.Adapter<LocalFileAdapter.LocalFileHolder>() {
    private val mInflater by lazy { LayoutInflater.from(mContext) }

    override fun getItemViewType(position: Int): Int {
        return when (type) {
            LocalFileConst.IMAGE -> {
                R.layout.local_file_item_picture
            }
            else -> R.layout.local_file_item_file
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocalFileHolder {
        return LocalFileHolder(mInflater.inflate(viewType, parent, false))
    }

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: LocalFileHolder, position: Int) {
        dataList[position].run {
            when (getItemViewType(position)) {
                R.layout.local_file_item_file -> {
                    val ivCover = holder.getView<ImageView>(R.id.ivCover)
                    when (this@LocalFileAdapter.type) {
                        LocalFileConst.AUDIO -> {
                            ivCover.setImageResource(R.mipmap.local_audio)
                        }
                        LocalFileConst.DOCUMENT -> {
                            when (name.substring(name.lastIndexOf("."), name.length)) {
                                ".docx", ".doc" -> {
                                    ivCover.setImageResource(R.mipmap.local_doc)
                                }
                                ".pdf"->{
                                    ivCover.setImageResource(R.mipmap.local_pdf)
                                }
                                ".xlsx",".xls"->{
                                    ivCover.setImageResource(R.mipmap.local_exe)
                                }
                                ".ppt",".pptx"->{
                                    ivCover.setImageResource(R.mipmap.local_ppt)
                                }
                            }
                        }
                        LocalFileConst.OTHER -> {
                            if (name.endsWith(".txt")) {
                                ivCover.setImageResource(R.mipmap.local_txt)
                            } else {
                                ivCover.setImageResource(R.mipmap.local_unknow)
                            }
                        }
                        else->{
                            Glide.with(ivCover)
                                .load(Uri.fromFile(File(path)))
                                .into(ivCover)
                        }
                    }
                    holder.getView<TextView>(R.id.tvTitle).text = name
                    holder.getView<TextView>(R.id.tvSize).text =
                        Formatter.formatFileSize(mContext, fileSize)
                    holder.getView<TextView>(R.id.tvTime).text =
                        DateFormat.format("yyyy-MM-dd HH:mm:ss", lastModifyTime)
                    val ivSelectStatus=holder.getView<ImageView>(R.id.ivSelectStatus)
                    holder.itemView.setOnClickListener {
                        onItemClick(this,ivSelectStatus,position)
                    }
                }
                R.layout.local_file_item_picture -> {
                    bindImageData(holder, position)
                }
            }
        }
    }

    /**
     * 绑定图片类型数据
     * */
    private fun LocalFileIm.bindImageData(holder: LocalFileHolder, position: Int) {
        val ivSelectStatus = holder.getView<ImageView>(R.id.ivSelectStatus)
        ivSelectStatus.isSelected = isSelect

        val mImageView = holder.getView<ImageView>(R.id.mImageView)
        Glide.with(mImageView)
            .load(this.path)
            .placeholder(R.mipmap.image_default)
            .error(R.mipmap.image_default)
            .into(mImageView)

        mImageView.setOnClickListener {
            onItemClick(this, ivSelectStatus, position)
        }
    }

    class LocalFileHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val mView by lazy { SparseArray<View>() }
        fun <T : View> getView(viewId: Int): T {
            var view = mView[viewId]
            if (view == null) {
                view = itemView.findViewById(viewId)
                mView.put(viewId, view)
            }
            return view!! as T
        }
    }
}